<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BUIManager
 *
 * @author jianfeng
 */
class BUIScreen extends BUIControl {

    const Default_LayoutView = 'defaultScreenLayoutView';

    protected $screenParent = null;
    protected $uiNode = null;
    protected $perviousScreen = null;
    protected $nextScreen = null;
    protected $screenFlow = null;
    protected $node = null;

    public function __construct($parent = NULL) {

        parent::__construct();

        if ($parent != null)
            $this->screenParent = $parent;
    }

    /**
     *
     * @return BUINode 
     */
    public function getUINode() {
        if ($this->screenParent instanceof BUIScreenFlow)
            return $this->screenParent->getUINode();

        if ($this->uiNode == NULL && $this->node != NULL) {
            if ($this->node instanceof BNode)
                return $this->node->getUIManager();
        }

        return $this->uiNode;
    }

    public function getNode() {
        return Blyn::app()->getCurrentNode();
        //return $this->node;
    }

    public function setNode($node) {
        $this->node = $node;
    }

    /**
     *
     * @param type $uiNode 
     */
    public function setUINode($uiNode) {

        $uiNode = Blyn::app()->getCurrentNode()->getUIManager();

        if ($this->screenParent instanceof BUIScreenFlow)
            $this->screenParent->setUINode($uiNode);
        else {
            $this->uiNode = $uiNode;
        }
    }

    public function getParent() {
        return $this->screenParent;
    }

    public function setParent($parent) {
        if ($parent instanceof BUINode)
            $this->setUINode($parent);
        if ($parent instanceof BUIScreenFlow)
            $this->setScreenFlow($parent);

        $this->screenParent = $parent;
    }

    public function setScreenFlow($scnFlow) {
        $this->screenFlow = $scnFlow;
    }

    public function getScreenFlow($name) {
        return $this->screenFlow;
    }

    public function onRender($data = null) {
        $currentUINode = Blyn::app()->getCurrentNode()->getUIManager();
        $currentUINode->setCurrentScreen($this);
        $this->addRenderView('screenTopBar', 'screen_topBar');
    }

    public function getName() {
        if ($this->name == NULL) {
            $className = get_class($this);
            if (substr($className, 0, 7) == 'BScreen')
                $screenName = substr($className, 7);
            else {
                $screenName = $className;
            }
            return $screenName;
        }
        return $this->name;
    }

    public function isMemberOfScreenFlow() {

        $currentScreenFlow = Blyn::app()->getUIManager()->getCurrentScreenFlow();

        if ($currentScreenFlow instanceof BUIScreenFlow) {
            if (!$currentScreenFlow->getScreenByName($this->getName()))
                $this->setParent($currentScreenFlow);

            return TRUE;
        }

        return FALSE;
    }

    public function getUrl($data = NULL) {
        if ($data == NULL)
            $data = array();

        $data['blyn_scn'] = get_class($this);

        $ajaxUrl = $this->getAjaxUrl($data);

        if ($ajaxUrl != NULL)
            return $ajaxUrl;

        $url = $this->getUINode()->getUrl($data);

        return $url;
    }

    /*
      public function getAjaxLink($name, $data = NULL) {
      return parent::createAjaxLink($name, $this->getUrl(), BUIBase::ajaxContainter_Space, Blyn::app()->getActiveSpace()->getUIManager()->getLayoutView(), $data);
      }
     * 
     */

    public function startRender($config = NULL) {

        $this->postStartRender();

        $controller = yii::app()->getController();

        $request = yii::app()->request;

        //$views = array();

        if ($request->isAjaxRequest) {

            //first get ajaxLinkId from request
            $ajaxLinkId = isset($_REQUEST['b_ajaxLinkId']) ? $_REQUEST['b_ajaxLinkId'] : NULL;

            //then get view and view data for rendering according to ajaxLinkId
            if ($ajaxLinkId != NULL) {
                $currentAjaxView = Blyn::app()->getContextVariable('currentAjaxViews');

                if (is_array($currentAjaxView)) {
                    $view = $currentAjaxView[$ajaxLinkId]['view'];
                    $viewData = isset($currentAjaxView[$ajaxLinkId]['data']) ? $currentAjaxView[$ajaxLinkId]['data'] : NULL;
                    $this->renderView($view, $viewData);
                }
            }

            //save current screen's information into database for next using
            Blyn::app()->getContextObj()->saveContextVariable();

            yii::app()->end();
        }

        //$layoutView = isset($config['layoutView']) ? $config['layoutView'] : NULL;
        //$viewsData = isset($config['views']) ? $config['views'] : NULL;

        $controller->onRender("startPage2");
        // $controller->render(BUIBase::ajax_startView_application);
    }

    public function postStartRender() {

        $tranObj = Blyn::app()->getContextObj();

        //$ContextApp = Blyn::app()->getContextApp();
        $currentUINode = Blyn::app()->getCurrentNode()->getUIManager();

        if ($this->isMemberOfScreenFlow()) {
            $sf = $this->getParent();
            $sf->addContextVariable(BUIScreenFlow::ContextVARIABLE_SCREENFLOW_INDEX, $sf->getIndex());
            //$sf->addContextVariable(BContextObj::VariableName_Id, $sf->getId());
            //$sf->addContextVariable(BContextObj::VariableName_Class, $sf->getName());
            $tranObj->addContextVariable(BContextObj::Scope_Blyn, BContextObj::V_CurrentScreenFlowId, $sf->getId());
            $tranObj->addContextVariable(BContextObj::Scope_Blyn, BContextObj::V_CurrentScreenFlowName, $sf->getName());

            $tranObj->resetContextVariable(BContextObj::Scope_Screen);
        } else {
            $tranObj->resetContextVariable(BContextObj::Scope_ScreenFlow);
            //$tranObj->addContextVariable(BContextObj::VariableName_CurrentScreen, BContextObj::VariableName_Id, $this->getId());
            //$tranObj->addContextVariable(BContextObj::VariableName_CurrentScreen, BContextObj::VariableName_Class, get_class($this));
            $tranObj->addContextVariable(BContextObj::Scope_Blyn, BContextObj::V_CurrentScreenId, $this->getId());
            $tranObj->addContextVariable(BContextObj::Scope_Blyn, BContextObj::V_CurrentScreenName, get_class($this));
        }

        $tranObj->saveContextVariable();
    }

    public function getId() {
        $uiNodeId = Blyn::app()->getCurrentNode()->getUIManager()->getId();
        return $uiNodeId . '_' . $this->getName();
    }

    public function addContextVariable($name, $value, $scope = NULL, $type = NULL) {

        $tObj = Blyn::app()->getContextObj();

        /*
          if ($name == BContextObj::VariableName_Id)
          return $tObj->addContextVariable(BContextObj::Scope_AppMeta, BContextObj::V_CurrentScreenFlowId, $value);
          if ($name == BContextObj::VariableName_Class)
          return $tObj->addContextVariable(BContextObj::Scope_AppMeta, BContextObj::V_CurrentScreenFlowClass, $value);
         * 
         */

        $tObj->addContextVariable(BContextObj::Scope_Screen, $name, $value);
    }

    public function getContextVariable($name, $scope = NULL, $type = NULL) {

        $tObj = Blyn::app()->getContextObj();

        /*
          if ($name == BContextObj::VariableName_Id)
          return $tObj->getContextVariable(BContextObj::Scope_AppMeta, BContextObj::V_CurrentScreenFlowId);
          if ($name == BContextObj::VariableName_Class)
          return $tObj->getContextVariable(BContextObj::Scope_AppMeta, BContextObj::V_CurrentScreenFlowClass);
         * 
         */

        return $tObj->getContextVariable(BContextObj::Scope_Screen, $name);
    }

    public function resetContextVariable($name = NULL) {

        $tObj = Blyn::app()->getContextObj();
        /**
         * for scope reset, only happen when id changed
         */
        if ($name == NULL) {
            $tId = $this->getContextVariable(BContextObj::Current_Id);
            $myId = $this->getId();
            if ($tId != $myId) {
                $tObj->resetContextVariable(BContextObj::Scope_Screen);
                $this->addContextVariable(BContextObj::Current_Id, $myId);
            }
        } else {
            $tObj->resetContextVariable(BContextObj::Scope_Screen, $name);
        }
    }

    public function getAjaxContainerId($data = null) {
        //parent::getAjaxContainerId($data);
        return BUIBase::ajaxContainter_screen;
    }

    public function getAjaxLink($name, $data = array(), $sender = NULL, $router = 'site/index', $updateControl = NULL) {
        if ($this->getNode() != NULL && Blyn::app()->getCurrentNode()->getId() != $this->getNode()->getId())
            return $this->getNode()->getAjaxLink($name, $data, $sender, $router, $updateControl);
        else
            return parent::getAjaxLink($name, $data, $sender, $router, $updateControl);
    }

}

?>
